unit clsTaxa;

interface

uses Dm, dbtables, biblio;

type
  TTaxa = class(TObject)
  private
    Taxa_Periodo: string;
  public
    Taxa_Juro: Double;
    function Gravar(p_Taxa_Periodo: string): Byte;
    function Recuperar(p_Taxa_Periodo: string): Byte;
    function Excluir(p_Taxa_Periodo: string): Byte;
    procedure Consultar(var p_q: TQuery);
    procedure Consultar_Intervalo(var p_q: TQuery; p_taxaini, p_taxafim: string);
  end;

implementation

function TTaxa.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Taxa where Taxa_Periodo=:k_Taxa_Periodo');
  tq.parambyname('k_Taxa_Periodo').asstring := p_Taxa_Periodo;
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO Taxa VALUES(:k_Taxa_Periodo,:k_Taxa_Juro)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE Taxa SET Taxa_Juro=:k_Taxa_Juro WHERE Taxa_Periodo=:k_Taxa_Periodo');
  end;
  tq.parambyname('k_Taxa_Periodo').asstring := p_Taxa_Periodo;
  tq.parambyname('k_Taxa_Juro').AsFloat := Taxa_Juro;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TTaxa.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Taxa where Taxa_Periodo=:k_Taxa_Periodo');
  tq.Params[0].asstring := p_Taxa_Periodo;
  tq.open;
  Taxa_Periodo := p_Taxa_Periodo;
  if tq.eof then begin
    Taxa_Juro := 0;
    Result := 0;
  end else begin
    Taxa_Juro := tq.fieldbyname('Taxa_Juro').AsFloat;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TTaxa.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Taxa WHERE Taxa_Periodo=:k_Taxa_Periodo');
  tq.parambyname('k_Taxa_Periodo').asstring := p_Taxa_Periodo;
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TTaxa.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from Taxa order by Taxa_Periodo');
end;

procedure TTaxa.Consultar_Intervalo;

begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  //modificado em 07/03/2005
  //p_q.sql.add('Select * from Taxa where taxa_periodo>=:k_per_ini and taxa_periodo<=:k_per_fim order by Taxa_Periodo');
  p_q.sql.add('Select * from Taxa where ' +
    'substr(taxa_periodo,3,4)||substr(taxa_periodo,1,2) >=:k_per_ini and ' +
    'substr(taxa_periodo,3,4)||substr(taxa_periodo,1,2) <=:k_per_fim ' +
    'order by substr(taxa_periodo,3,4)||substr(taxa_periodo,1,2)');
  p_q.ParamByName('k_per_ini').asstring := p_taxaini;
  p_q.ParamByName('k_per_fim').asstring := p_taxafim;
end;

end.
